import 'package:shared_preferences/shared_preferences.dart';

// 查看了一下  shared_preferences 存储 map 类型比较麻烦 所以决定参单个储存的方式
// 增加
add(type, key, value) async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  switch (type) {
    case 'setString':
      prefs.setString(key, value);
      break;
    case 'setBool':
      prefs.setBool(key, value);
      break;
    case 'setDouble':
      prefs.setDouble(key, value);
      break;
    case 'setInt':
      prefs.setInt(key, value);
      break;
    default:
      prefs.setStringList(key, value);
  }
}

// 查询
show(type, key) async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  switch (type) {
    case 'getString':
      return prefs.getString(key);
    case 'getBool':
      return prefs.getBool(key);
    case 'getDouble':
      return prefs.getDouble(key);
    case 'getInt':
      return prefs.getInt(key);
    default:
      return prefs.getStringList(key);
  }
}

// 删除
dele(key) async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  prefs.remove(key); //删除指定键
}

// 彻底清空shared_preferences
clear() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  prefs.clear(); //清空键值对
}
